﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using IP.Core.IPBusinessService;
using IP.Core.IPCommon;
using IP.Core.IPData;
using IP.Core.IPUserService;
using WebDS;
using WebDS.CDBNames;
using WebUS;
using System.Data;

public partial class ChucNang_Don_xin_nghi_phep : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            load_cbo_hinh_thuc_nghi();
            load_cbo_nhan_vien();
            load_grv_dm_nghi_phep();
            format_form();
        }
    }

    #region Members
    DS_GD_NGHI_PHEP m_ds_nghi_phep = new DS_GD_NGHI_PHEP();
    US_GD_NGHI_PHEP m_us_nghi_phep = new US_GD_NGHI_PHEP();
    #endregion

    #region Data Structures

    #endregion

    #region Public Methods

    #endregion

    #region Private Methods
    private void obj_to_form(US_GD_NGHI_PHEP ip_us_nghi_phep)
    {

        m_cbo_user.SelectedValue = ip_us_nghi_phep.dcID_NHAN_VIEN.ToString();
        if (!ip_us_nghi_phep.IsNGHI_TU_NGAYNull() || ip_us_nghi_phep.datNGHI_TU_NGAY != CIPConvert.ToDatetime("01/01/1900", "dd/MM/yyyy"))
            m_dat_nghi_tu.SelectedDate = ip_us_nghi_phep.datNGHI_TU_NGAY;
        if (!ip_us_nghi_phep.IsDEN_NGAYNull() || ip_us_nghi_phep.datDEN_NGAY != CIPConvert.ToDatetime("01/01/1900", "dd/MM/yyyy"))
            m_dat_nghi_den.SelectedDate = ip_us_nghi_phep.datDEN_NGAY;
        m_cbo_hinh_thuc_nghi.SelectedValue = ip_us_nghi_phep.dcID_HINH_THUC_NGHI.ToString();
        m_txt_ly_do.Text = ip_us_nghi_phep.strLY_DO;
    }

    private void form_to_obj()
    {
        System.Globalization.CultureInfo enUS = new System.Globalization.CultureInfo("en-US");

        m_us_nghi_phep.dcID_NHAN_VIEN = CIPConvert.ToDecimal(m_cbo_user.SelectedValue);
        m_us_nghi_phep.dcID_HINH_THUC_NGHI = CIPConvert.ToDecimal(m_cbo_hinh_thuc_nghi.SelectedValue);
        m_us_nghi_phep.strLY_DO = m_txt_ly_do.Text;
        // Kiểm tra đúng định dạng datetime
        DateTime v_dat_out_result;
        if (DateTime.TryParseExact(CIPConvert.ToStr(m_dat_nghi_tu.SelectedDate), "dd/MM/yyyy", enUS, System.Globalization.DateTimeStyles.None, out v_dat_out_result))
        {
            if (m_dat_nghi_tu.SelectedDate != CIPConvert.ToDatetime("01/01/0001"))
                m_us_nghi_phep.datNGHI_TU_NGAY = m_dat_nghi_tu.SelectedDate;
            else m_us_nghi_phep.SetNGHI_TU_NGAYNull();
        }

        if (DateTime.TryParseExact(CIPConvert.ToStr(m_dat_nghi_den.SelectedDate), "dd/MM/yyyy", enUS, System.Globalization.DateTimeStyles.None, out v_dat_out_result))
        {
            if (m_dat_nghi_den.SelectedDate != CIPConvert.ToDatetime("01/01/0001"))
                m_us_nghi_phep.datDEN_NGAY = m_dat_nghi_den.SelectedDate;
            else m_us_nghi_phep.SetDEN_NGAYNull();
        }
    }    
    private void load_cbo_hinh_thuc_nghi()
    {
        US_CM_DM_TU_DIEN v_us_hinh_thuc_nghi = new US_CM_DM_TU_DIEN();
        DS_CM_DM_TU_DIEN v_ds_hing_thuc_nghi = new DS_CM_DM_TU_DIEN();
        v_us_hinh_thuc_nghi.fill_tu_dien_cung_loai_ds("HINH_THUC_NGHI_PHEP" ,v_ds_hing_thuc_nghi);
        m_cbo_hinh_thuc_nghi.DataSource = v_ds_hing_thuc_nghi.CM_DM_TU_DIEN;
        m_cbo_hinh_thuc_nghi.DataTextField = CM_DM_TU_DIEN.TEN;
        m_cbo_hinh_thuc_nghi.DataValueField = CM_DM_TU_DIEN.ID;
        m_cbo_hinh_thuc_nghi.Visible = true;
        m_cbo_hinh_thuc_nghi.DataBind();
    }
    private void load_grv_dm_nghi_phep()
    {
        US_V_GD_NHAN_VIEN_NGHI_PHEP v_us_dm_nghi_phep = new US_V_GD_NHAN_VIEN_NGHI_PHEP();
        DS_V_GD_NHAN_VIEN_NGHI_PHEP v_ds_dm_nghi_phep = new DS_V_GD_NHAN_VIEN_NGHI_PHEP();
        v_us_dm_nghi_phep.FillDataset(v_ds_dm_nghi_phep);
        if (v_ds_dm_nghi_phep.V_GD_NHAN_VIEN_NGHI_PHEP != null && v_ds_dm_nghi_phep.V_GD_NHAN_VIEN_NGHI_PHEP.Rows.Count > 0)
        {
            m_grv_dm_nghi_phep.DataSource = v_ds_dm_nghi_phep.V_GD_NHAN_VIEN_NGHI_PHEP;
            m_grv_dm_nghi_phep.DataBind();
        }
    }
    private void load_cbo_nhan_vien()
    {
        try
        {
            US_V_DM_NHAN_VIEN v_us_nv = new US_V_DM_NHAN_VIEN();
            DS_V_DM_NHAN_VIEN v_ds_nv = new DS_V_DM_NHAN_VIEN();
            v_us_nv.FillDataset(v_ds_nv);
            m_cbo_user.DataSource = v_ds_nv.V_DM_NHAN_VIEN;
            m_cbo_user.DataTextField = V_DM_NHAN_VIEN.HO_VA_TEN;
            m_cbo_user.DataValueField = V_DM_NHAN_VIEN.ID;
            m_cbo_user.DataBind();
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    private void load_update_dm_nghi_phep(int i_int_row_index)
    {
        try
        {
            decimal v_dc_id_dm_nghi_phep = CIPConvert.ToDecimal(m_grv_dm_nghi_phep.DataKeys[i_int_row_index].Value);
            US_GD_NGHI_PHEP v_us_dm_nghi_phep = new US_GD_NGHI_PHEP(v_dc_id_dm_nghi_phep);
            m_hdf_id_nghi_phep.Value = CIPConvert.ToStr(v_dc_id_dm_nghi_phep);
            obj_to_form(v_us_dm_nghi_phep);
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    private void delete_dm_nghi_phep(int i_int_row_index)
    {
        try
        {
            decimal v_dc_id_dm_nghi_phep = CIPConvert.ToDecimal(m_grv_dm_nghi_phep.DataKeys[i_int_row_index].Value);
            m_us_nghi_phep.DeleteByID(v_dc_id_dm_nghi_phep);
            load_grv_dm_nghi_phep();
            m_lbl_thong_bao.Text = "Xóa bản ghi thành công.";
            m_lbl_thong_bao.Visible = true;
        }
        catch (Exception v_e)
        {
            m_lbl_thong_bao.Text = "Lỗi trong quá trình xóa bản ghi.";
            m_lbl_thong_bao.Visible = true;
            throw v_e;
        }
    }
    private void insert_dm_nghi_phep()
    {
        try
        {
            m_grv_dm_nghi_phep.EditIndex = -1;
            if (Page.IsValid)
            {
                if (!check_validate())
                {
                    m_lbl_thong_bao.Text = "Có lỗi trong quá trình tạo phép";
                    return;
                }
                form_to_obj();
                m_us_nghi_phep.Insert();
                m_lbl_thong_bao.Text = "Đã thêm mới thành công.";
                m_lbl_thong_bao.Visible = true;
                clear_dm_nghi_phep();
                load_grv_dm_nghi_phep();
            }
        }
        catch (Exception v_e)
        {
            m_lbl_thong_bao.Text = "Lỗi trong quá trình thêm mới bản ghi.";
            m_lbl_thong_bao.Visible = true;
            throw v_e;
        }
    }
    private void update_dm_nghi_phep()
    {
        try
        {
            if (Page.IsValid)
            {
                if (this.m_hdf_id_nghi_phep.Value == "")
                {
                    m_lbl_thong_bao.Text = "Bạn phải chọn đơn nghỉ cần Cập nhật.";
                    m_lbl_thong_bao.Visible = true;
                    return;
                }
                if (!check_validate())
                {
                    m_lbl_thong_bao.Text = "Có lỗi trong quá trình cập nhật phép.";
                    return;
                }
                form_to_obj();
                m_us_nghi_phep.dcID = CIPConvert.ToDecimal(this.m_hdf_id_nghi_phep.Value);
                m_us_nghi_phep.Update();
                m_lbl_thong_bao.Text = "Đã cập nhật bản ghi thành công.";
                clear_dm_nghi_phep();
                m_grv_dm_nghi_phep.EditIndex = -1;
                load_grv_dm_nghi_phep();
            }
        }
        catch (Exception v_e)
        {
            m_lbl_thong_bao.Text = "Lỗi trong quá trình cập nhật bản ghi.";
            throw v_e;
        }
    }
    private void clear_dm_nghi_phep()
    {
        try
        {
            m_grv_dm_nghi_phep.EditIndex = -1;
            m_txt_ly_do.Text = "";
            m_dat_nghi_den.Text = "";
            m_dat_nghi_tu.Text = "";
            this.m_hdf_id_nghi_phep.Value = "";
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    private bool check_validate()
    {
        System.Globalization.CultureInfo enUS = new System.Globalization.CultureInfo("en-US");
        if (this.m_cbo_user.SelectedIndex == -1 || this.m_cbo_hinh_thuc_nghi.SelectedIndex == -1)
        {            
            return false;
        }

        DateTime v_dat_from;
        DateTime v_dat_to;
        if (m_dat_nghi_tu.ValidDateEntered && m_dat_nghi_den.ValidDateEntered)
        {
            v_dat_from = m_dat_nghi_tu.SelectedDate;
            v_dat_to = m_dat_nghi_den.SelectedDate;
        }
        else
        {
            return false;
        }

        if (v_dat_from > v_dat_to)
        {
            return false;
        }
        
        return true;
    }

    private void format_form()
    {
        m_cbo_user.SelectedIndex = 0;
        m_cbo_hinh_thuc_nghi.SelectedIndex = 0;
    }
    #endregion

    #region Events
    protected void m_cbo_user_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {            
            load_grv_dm_nghi_phep();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_grv_dm_nghi_phep_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            m_lbl_thong_bao.Text = "";
            delete_dm_nghi_phep(e.RowIndex);
        }
        catch (Exception v_e)
        {
            // de su dung CsystemLog_301 bat buoc Site phai dat trong thu muc cap 1. Vi du: DanhMuc/Dictionary.aspx
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_grv_dm_nghi_phep_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        try
        {
            m_lbl_thong_bao.Text = "";
            load_update_dm_nghi_phep(e.NewSelectedIndex);
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_cmd_tao_moi_Click(object sender, EventArgs e)
    {
        try
        {
            m_lbl_thong_bao.Text = "";
            insert_dm_nghi_phep();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_btn_cap_nhat_Click(object sender, EventArgs e)
    {
        try
        {
            m_lbl_thong_bao.Text = "";
            update_dm_nghi_phep();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_btn_xoa_trang_Click(object sender, EventArgs e)
    {
        try
        {
            clear_dm_nghi_phep();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    #endregion    
}